package plugin.redesign;

import gui.ConfirmWindow;

import java.io.File;
import java.util.List;
import java.util.Map;

import models.action.Action;
import models.asl.Graph;
import models.asl.Node;
import models.state.EnvStateGraph;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import reader.ActionReader;
import reader.TextEnvStateTransReader;
import reader.Transformation;
import reader.UMLReader;
import redesign.Redesign;
import writer.ASLFileWriter;
import check.Checker;

public class RedesignHandler implements IObjectActionDelegate{
    private static String filePath;
    public static String projectPath;
    public static String fileName;
    @Override
    public void run(IAction arg0) {
        System.out.println("Start Redesign");
        generateSuggestion(null);
    }

    @Override
    public void selectionChanged(IAction arg0, ISelection arg1) {
        if(arg1 instanceof IStructuredSelection) {
            Object obj = ((IStructuredSelection) arg1).getFirstElement();
            IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
                    IFile.class);
            if (file == null) {
                if (obj instanceof IAdaptable) {
                    file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
                }
            }
            if(file != null) {
                filePath = file.getRawLocation().toOSString();
                fileName = file.getName();
                projectPath = file.getProject().getLocation().toOSString();
            }

        }
    }

    @Override
    public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * Generate suggestion.
     */
    public static void generateSuggestion(String redesignedFilePath) {
        try {

            UMLReader umlReader = new UMLReader();
            TextEnvStateTransReader textEnvReader = new TextEnvStateTransReader();

            String umlFilePath = projectPath + File.separator + "temp" + File.separator + "model.uml";
            String textEnvFilePath = projectPath + File.separator + "temp" + File.separator + "model.txt";
//            for (String aslFile : fileLists) {
//                String aslFileNameFromPath = aslFile.substring(aslFile
//                        .lastIndexOf("\\") + 1);
//                if (aslFileNameFromPath != null
//                        && aslFileNameFromPath
//                                .equalsIgnoreCase(MainDriver.stateTranDiagramName)) {
//                    umlFilePath = aslFile;
//                    break;
//                }
//            }
            EnvStateGraph graph = null;
            try {
                graph = umlReader.parseUMLFile(umlFilePath);
            } catch(Exception e) {
                graph = textEnvReader.readEnvStateTransDiagram(textEnvFilePath);
            }
            //
            // for(String aslFile : fileLists) {
            // String aslFileNameFromPath = aslFile.substring(aslFile.lastIndexOf("\\") + 1);
            // if(aslFileNameFromPath != null && aslFileNameFromPath.equalsIgnoreCase(MainDriver.aslFileName)) {
            // aslFilePath = aslFile;
            // break;
            // }
            // }

            // read pre&post condition of actions
            ActionReader ar = new ActionReader();
            List<Action> actions;

            actions = ar.readActions(projectPath + File.separator + "temp"
                    + File.separator + fileName + ".action");

            Transformation transformation = new Transformation(redesignedFilePath != null ? redesignedFilePath : filePath);
            List<Graph> domainGraph;
            domainGraph = transformation.generate();
            Map<Node, List<String>> requireCheck = null;
            requireCheck = Checker
                        .checkContextConditionRequired(graph,
                                domainGraph.get(0), actions);
            if(requireCheck != null && !requireCheck.isEmpty()) {
                System.out.println(requireCheck);
                Redesign.redesignGraph(domainGraph.get(0), requireCheck);
                System.out.println("After Redesign:" + domainGraph.get(0));
                ConfirmWindow cw = new ConfirmWindow();
                ASLFileWriter.writeToConfirmWindow(cw, domainGraph.get(0));
                cw.setVisible(true);
            }
//            ASLFileWriter.writeASLFile(projectLocation + File.separator + "src"+ File.separator + "redesign_"
//                    + MainDriver.aslFileName, domainGraph.get(0));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
